/**
 *@description:	定义XOMS的命名空间, 所有自己抽象的插件都将基于此js
 *				另外, 所有页面都会使用的方法也在此定义运行, 如导航相关, 语言切换, 回到顶部
 *@author:	yanxiaoe 
 *@example 直接在define中引用,不需要专门调用
 */
define(['jquery', 'jquery.placeholder'], function($) {
	/**
	 * @namespace
	 */
	var XOMS = XOMS || {};
	XOMS.contextPath = $('input[name="xoms-contextPath"]').val();
	
	// 全局ajax参数设置
	$.ajaxSetup({
		cache: false,
		contentType: "application/x-www-form-urlencoded;charset=utf-8",  
		error: function(){

		},
	    complete: function(XMLHttpRequest, textStatus){  
	        //通过XMLHttpRequest取得响应头，sessionstatus，  
	        var sessionstatus = XMLHttpRequest.getResponseHeader("sessionstatus");   
	        if(sessionstatus == "timeout"){  
	        	//如果超时就处理 ，指定要跳转的页面  
	            window.location = XOMS.contextPath + '/login.jsp';  
	        }
	    } 
	});
	
	var url = location.pathname;
	/* 导航相关 */
	XOMS.highlight = function() {
		var arr = url.split('/'),
		len = arr.length,
		topNav = arr[len - 2] == "" ? "home" : arr[len - 2],
		secNav = arr[len - 1] == "" ? "index" : arr[len - 1],
		navbar = $('.xoms-navbar-container');
		// 一级菜单高亮
		var $topNav = navbar.find('.' + topNav);
		$topNav.addClass('active');
		// 二级菜单高亮
		navbar.find('.' + topNav + secNav).addClass('active');
		// 我的订单 -> 订单管理中的三级菜单高亮
		$('.order-management-thirdnav').find('.' + secNav).addClass('active');
		// 我的订单 -> 创建订单中的三级菜单高亮
		$('.order-create-thirdnavbar').find('.' + secNav).addClass('active');
		
	};
	
	/* 屏幕小于768时,二级和三级菜单的滚动按钮事件  */
	XOMS.navScroll = function() {
		// 计算每一个导航的宽度
		var scrollLeft = 0,
		scrollLeftNum = 1,
		scrollNavContainer = $('.xoms-secnavbar .container');
		scrollNavContainerWidth = scrollNavContainer.width();
		
		var caculateScrollWidth = function($this) {
			var width = $this.width();
			scrollLeft += width;
			if(Math.abs(scrollNavContainerWidth - scrollLeft) < 40) {
				scrollNavContainer.attr('data-left-' + scrollLeftNum, scrollLeft - width);
				scrollLeftNum++;
			}
		};
		
		$('.xoms-secnavbar ul li').each(function() {
			var $this = $(this);
			caculateScrollWidth($this);
		});
		
		scrollLeft = 0,
		scrollLeftNum = 1,
		scrollNavContainer = $('.order-management-thirdnav');
		scrollNavContainerWidth = scrollNavContainer.width(),
		$('.order-management-thirdnav ul li').each(function() {
			var $this = $(this);
			caculateScrollWidth($this);
		});
		$('.scroll-div').on('click', 'a', function(e) {
			var $this = $(e.target),
			$parent = $this.parents('.scroll-div'),
			$navContainer = $parent.parent().find('.container'),
			$navUl = $navContainer.find('ul'),
			scrollNum = $parent.attr('data-scrollNum');
			if($this.hasClass('left') || $this.parent().hasClass('left')) {
				if(!scrollNum) {
					scrollNum = 1;
				} else {
					scrollNum = +scrollNum + 1;
				}
				var left = $navContainer.attr('data-left-' + scrollNum);
				if(!!left) {
					$navUl.css({
						left: '-' + left + 'px'
					});
				} else {
					scrollNum = +scrollNum - 1;
				}
			} else if($this.hasClass('right') || $this.parent().hasClass('right')) {
				if(!scrollNum || scrollNum == '0') {
					scrollNum = 0;
				} else {
					scrollNum = +scrollNum + 1;
				}
				var left = $navContainer.attr('data-left-' + scrollNum);
				if(!left) {
					left = 0;
					scrollNum = 0;
				}
				$navUl.css({
					left: '-' + left + 'px'
				});
			}
			$parent.attr('data-scrollNum', scrollNum);
		});
	};
	
	/* 语言切换相关 */
	XOMS.langChange = function () {
		var changeUl = $('.xoms-lang-change');
		changeUl.find('.xoms-lang-zh').attr('href', url + '?locale=zh_CN');
		changeUl.find('.xoms-lang-en').attr('href', url + '?locale=en_US');
		// 切换语言时,清空本地的缓存,为了国际化
		changeUl.on('click', 'a', function(e) {
			if(XOMS.hasOwnProperty('storage')) {
				XOMS.storage.clear();
			}
		});
	};
	
	
	/* 回到顶部  */
	XOMS.scrollTop = function(settings) {
		var defaultSettings = {
				showHeight: 100
			},
		_settings = $.extend({}, defaultSettings, settings),
		element = $('.xoms-scrolltop');
//		element = $('<div class="xoms-scroll-top" title="回到顶部"><span class="glyphicon glyphicon-plane"></span></div>');
//		$('body').append(element);
		element.click(function(){
			var itl = -1;
			itl = setInterval(function(){
				var scrollHeight = $(window).scrollTop();
				var spacing = Math.ceil(scrollHeight*0.2);
				if((scrollHeight-spacing)<10) {
					$(window).scrollTop(0);
					clearInterval(itl);
				} else {
					$(window).scrollTop(scrollHeight-spacing);
				}
			}, 10);
			return false;
		});
		
		$(window).scroll(function() {
			var scrollHeight = $(window).scrollTop();
			if(scrollHeight > _settings.showHeight) {
				element.show();
			} else {
				element.hide();
			}
		});
	};
	
	XOMS.init = function() {
		XOMS.highlight();
		XOMS.langChange();
		XOMS.navScroll();
		XOMS.scrollTop();
		// IE 9，添加placeholder
		$('input, textarea').placeholder({customClass:'xoms-placeholder'});
	};
	
	XOMS.init();
	
	return XOMS;
});